<?php
/**
 * @filesource						AdminUserAction.class.php
 * @desc							管理员用户管理
 * 
 * @author							houyining
 * @copyright						hou.yining@gmail.com
 * @date							2011-04-19
 */

class AdminUserAction extends CommonAction{
	public function index(){
		$crumb_info = array(
			array('uri' => '__URL__', 'title' => L('admin_user_manage'), 'sort' => C('CRUMB_LAST')),
		);
		
		$this->_mergeCrumbInfo($crumb_info);
		$this->_action_link = array(
			array('uri' => '__URL__/add', 'title' => L('add_admin_user')),
		);

		$this->assign('crumb_info', $this->_crumb_info);
		$this->assign('action_link', $this->_action_link);
		parent::index();
	}
	
	public function add(){
		$crumb_info = array(
			array('uri' => '__URL__', 'title' => L('admin_user_manage'), 'sort' => C('CRUMB_DEFAULT')),
			array('uri' => '__URL__/add', 'title' => L('add_admin_user'), 'sort' => C('CRUMB_LAST')),
		);
		
		$this->_mergeCrumbInfo($crumb_info);
		$this->_action_link = array(
			array('uri' => '__URL__/index', 'title' => L('list_admin_user')),
			);
		
		$this->assign('crumb_info', $this->_crumb_info);
		$this->assign('action_link', $this->_action_link);
		parent::add();
	}
	
	public function edit(){
		$crumb_info = array(
			array('uri' => '__URL__', 'title' => L('admin_user_manage'), 'sort' => C('CRUMB_DEFAULT')),
			array('uri' => '__URL__/edit/id/' . $_REQUEST['id'], 'title' => L('edit_admin_user'), 'sort' => C('CRUMB_LAST')),
		);

		$this->_mergeCrumbInfo($crumb_info);
		$this->_action_link = array(
			array('uri' => '__URL__/index', 'title' => L('list_admin_user')),
			);
		
		$this->assign('crumb_info', $this->_crumb_info);
		$this->assign('action_link', $this->_action_link);
		parent::edit();
	}
	
	public function showRoles(){
		$admin_user_id = $_REQUEST['admin_user_id'];
		
		$crumb_info = array(
			array('uri' => '__URL__', 'title' => L('admin_user_manage'), 'sort' => C('CRUMB_DEFAULT')),
			array('uri' => '__URL__/showRoles/admin_user_id/' . $_REQUEST['admin_user_id'], 'title' => L('show_roles'), 'sort' => C('CRUMB_LAST')),
		);

		$this->_mergeCrumbInfo($crumb_info);		
		$this->assign('crumb_info', $this->_crumb_info);
		
		//得到当前用户下的角色列表
		$role_user = new RoleUserModel();
		$role_user_list_v = $role_user->field('role_id')->where(array('user_id' => $admin_user_id))->select();
		$role_user_list = array();
		
		foreach($role_user_list_v as  $v){
			$role_user_list[$v['role_id']] = $v['role_id'];
		}
		
		//得到所有的角色
		$role = new RoleModel();
		$role_list  = $role->field('id,name')->select();
		
		$this->assign('admin_user_id', $admin_user_id);
		$this->assign('role_user_list', $role_user_list);
		$this->assign('role_list', $role_list);
		$this->display();
	}
	
	public function addRoles(){
		$admin_user_id = $_REQUEST['admin_user_id'];
		$user_roles_id = $_REQUEST['user_roles_id'];
		
		$role_user = new RoleUserModel();
		$role_user->deleteUserRole($admin_user_id);
		
		$data = array('user_id' => $admin_user_id,
					  '__hash__' => $_REQUEST['__hash__']);	

		foreach($user_roles_id as $role_id){
			$data['role_id']	= $role_id;
			if(false === $role_user->create($data)){
				$this->_assign_fail();
			}
			if(false === $role_user->add()){
				$this->_assign_fail();
			}
		}
		$this->_assign_success();
	}	
}
?>